/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.edu.gobesco.managedbean;

import co.com.edu.gobesco.controller.TipoPersonaJpaController;
import co.com.edu.gobesco.controller.exceptions.PreexistingEntityException;
import co.com.edu.gobesco.entityes.TipoPersona;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author Edwin Chía
 * @Fecha: 19/10/2011
 */


@ManagedBean
@RequestScoped
public class PersonasBean implements Serializable{

     private static final long serialVersionUID = -3832235132261771583L;

    /** Creates a new instance of PersonasBean */
    private String pageInclude;

    public String getPageInclude() {
	return pageInclude;
    }

    public void setPageInclude(String pageInclude) {
	this.pageInclude = pageInclude;
    }

    public String getDescTipoPersona() {
	return descTipoPersona;
    }

    public void setDescTipoPersona(String descTipoPersona) {
	this.descTipoPersona = descTipoPersona;
    }
    private String descTipoPersona;
    private boolean regTipoPer;
    private boolean editTipoPer;

    BigDecimal idTipoPer;

    public BigDecimal getCurrentPerEdit() {
	return currentPerEdit;
    }

    public void setCurrentPerEdit(BigDecimal currentPerEdit) {
	this.currentPerEdit = currentPerEdit;
    }

    BigDecimal currentPerEdit;

    public BigDecimal getIdTipoPer() {
	return idTipoPer;
    }

    public void setIdTipoPer(BigDecimal idTipoPer) {
	this.idTipoPer = idTipoPer;
    }

    TipoPersona tipopersonaVO;

    public TipoPersona getTipopersonaVO() {
	return tipopersonaVO;
    }

    public void setTipopersonaVO(TipoPersona tipopersonaVO) {
	this.tipopersonaVO = tipopersonaVO;
    }

    public boolean isEditTipoPer() {
	return editTipoPer;
    }

    public void setEditTipoPer(boolean editTipoPer) {
	this.editTipoPer = editTipoPer;
    }

    public boolean isRegTipoPer() {
	return regTipoPer;
    }
    private boolean flag;

    public boolean isFlag2() {
	return flag2;
    }

    public void setFlag2(boolean flag2) {
	this.flag2 = flag2;
    }
    private boolean flag2;

    public boolean isFlag() {
	return flag;
    }

    public void setFlag(boolean flag) {
	this.flag = flag;
    }

    public int getPage() {
	return page;
    }

    public void setPage(int page) {
	this.page = page;
    }

    public boolean isRecarguePag() {
	return recarguePag;
    }

    public void setRecarguePag(boolean recarguePag) {
	this.recarguePag = recarguePag;
    }


    private boolean recarguePag;
    
     private int page = 1;

    public void setRegTipoPer(boolean regTipoPer) {
	this.regTipoPer = regTipoPer;
    }
    List<TipoPersona> listaTipoPers;

    public List<TipoPersona> getListaTipoPers() {

	TipoPersonaJpaController jpaCtrl = new TipoPersonaJpaController();
	//if( listaTipoPers == null )
	this.listaTipoPers = jpaCtrl.findTipoPersonaEntities();

	System.out.println("listaPers: " + listaTipoPers);

	return listaTipoPers;
    }

    public void setListaTipoPers(List<TipoPersona> listaTipoPers) {
	this.listaTipoPers = listaTipoPers;
    }

    

    public void registrarTipoPersona() {
	this.setFlag(true);
	System.out.println("registrarTipoPersona init... ");
	TipoPersonaJpaController jpaCtrl = new TipoPersonaJpaController();
	TipoPersona typePer = new TipoPersona();
	typePer.setDescripcion(this.descTipoPersona);
	try {
	    jpaCtrl.create(typePer);
	    this.setRegTipoPer(true);
	} catch (PreexistingEntityException ex) {
	    this.setRegTipoPer(false);
	    Logger.getLogger(PersonasBean.class.getName()).log(Level.SEVERE, null, ex);
	} catch (Exception ex) {
	    this.setRegTipoPer(false);
	    Logger.getLogger(PersonasBean.class.getName()).log(Level.SEVERE, null, ex);
	}
	System.out.println("registrarTipoPersona regTipoPer: " + regTipoPer);

	this.setListaTipoPers(null);

    }





    public void editarTipoPersona() {
	this.setFlag2(true);
	
	System.out.println("idTipoPer_: " + tipopersonaVO.getIdTipopersona());
	System.out.println("descTipoPersona_: " + tipopersonaVO.getDescripcion());

	if (tipopersonaVO.getIdTipopersona() != null) {

	    System.out.println("editarTipoPersona init... ");
	    TipoPersonaJpaController jpaCtrl = new TipoPersonaJpaController();	    
	    try {
		jpaCtrl.edit( tipopersonaVO );
		this.setEditTipoPer(true);
	    } catch (PreexistingEntityException ex) {
		this.setEditTipoPer(false);
		Logger.getLogger(PersonasBean.class.getName()).log(Level.SEVERE, null, ex);
	    } catch (Exception ex) {
		this.setRegTipoPer(false);
		Logger.getLogger(PersonasBean.class.getName()).log(Level.SEVERE, null, ex);
	    }

	}
	System.out.println("editarTipoPersona regTipoPer: " + editTipoPer);
	this.setListaTipoPers(null);

    }
}
